<template>
  <div class="block">
    <div class="viewMain">
      <el-divider>
        <span class="title">{{ $t('apiList.apiDetail') }}</span>
      </el-divider>
      <div class="message">
        <p class="item">{{ $t('apiList.sign') }}：{{ detail.infName }}</p>
        <p class="item">{{ $t('apiList.name') }}：{{ detail.infNameName }}</p>
        <p class="item">
          {{ $t('apiList.method') }}：
          <span class="red">POST</span>
        </p>
        <!-- 
              <p class="item">服务提供商：</p>-->
        <p class="item">{{ $t('apiList.desc') }}：{{ detail.infDescription }}</p>
        <div class="codeTemplate">
          <pre class="code orange">/business/custom/dynamic/execute</pre>
        </div>
        <p class="item">{{ $t('apiList.requestHeader') }}</p>
        <div class="table">
          <ParamsTable :data="requestHeader" />
        </div>
        <p class="item">{{ $t('apiList.requestBody') }}</p>
        <div class="table">
          <ParamsTable :data="sqlRequestParams" />
        </div>
        <p class="item">{{ $t('apiList.requestReturn') }}</p>
        <div class="table">
          <ResponseTable :data="responseParams" />
        </div>
        <p class="item">{{ $t('apiList.requestReturnExample') }}</p>
        <div v-if="responseDemo" class="jsonTemplate">
          <vue-json-editor v-model="responseDemo" :mode="'code'" :modes="['code']" :expanded-on-start="true" :show-btns="false" lang="zh" />
        </div>
        <div v-else class="codeTemplate" style="margin: 10px 0">
          <pre class="code">{{ $t('apiList.notData') }}</pre>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { pageList, interfaceDetail } from '@/api/generator/interface'
import ParamsTable from './paramsTable'
import ResponseTable from './responseTable'
import vueJsonEditor from 'vue-json-editor'

export default {
  name: 'ApiList',
  components: { ParamsTable, ResponseTable, vueJsonEditor },
  data() {
    return {
      requestHeader: [
        {
          paramName: 'Inf-App-Id',
          dataType: 'String',
          description: '应用唯一标识appId',
          source: '平台',
          require: '必填',
        },
        {
          paramName: 'Inf-App-Secret',
          dataType: 'String',
          description: '接口秘钥',
          source: '平台',
          require: '必填',
        },
      ],
      responseParams: [],
      sqlRequestParams: [],
      responseDemo: null,
      detail: {},
      id: this.$route.params.id,
    }
  },
  mounted() {
    this.toViewDetail()
  },
  methods: {
    matchParams(params, id = '') {
      if (params) {
        let data = null
        if (typeof params === 'string') {
          data = JSON.parse(params)
        } else if (typeof params === 'object') {
          data = params
        }
        return data.map((item, idx) => {
          const idString = (id ? id + '-' : '') + idx
          return {
            ...item,
            id: idString,
            children: item.children ? this.matchParams(item.children, idString) : null,
          }
        })
      } else {
        return []
      }
    },
    toViewDetail() {
      interfaceDetail(this.id).then(({ data }) => {
        const sqlRequestParams = this.matchParams(data.sqlRequestParams)
        const responseParams = this.matchParams(data.responseParams)
        this.responseParams = [
          {
            map: 'code',
            type: 'string',
            id: '0-1',
            desc: '平台响应码',
            source: '平台',
          },
          {
            map: 'message',
            type: 'string',
            id: '0-2',
            desc: '平台响应信息',
            source: '平台',
          },
          {
            map: 'data',
            type: 'Object',
            desc: '接口返回的数据',
            source: '平台',
            id: '0-3',
            children: responseParams,
          },
        ]
        this.sqlRequestParams = sqlRequestParams
        const responseDemo = data.responseDemo
        if (responseDemo && typeof responseDemo === 'string') {
          this.responseDemo = JSON.parse(responseDemo)
        } else {
          this.responseDemo = null
        }
        this.detail = data
      })
    },
  },
}
</script>

<style scoped lang="scss">
.viewMain {
  overflow: hidden;
  padding: 0 20px;
  h2 {
    margin: 0 0 20px;
    font-size: 20px;
    line-height: 60px;
    border-bottom: 1px solid #dcdfe6;
    padding-left: 10px;
  }
  .message {
    overflow: hidden;
  }
  .title {
    font-size: 16px;
  }
  p {
    color: #666;
    font-size: 14px;
    margin: 0;
  }
  .red {
    color: rgb(249, 38, 114);
  }
  .orange {
    color: #e29629;
  }
  .black {
    color: #000;
  }
  .item {
    margin-top: 20px;
  }
  .codeTemplate {
    background: #f7f7f7;
    font-size: 14px;
    color: #333;
    padding: 20px;
    margin-top: 20px;
  }
  .jsonTemplate {
    margin: 10px 0;
    overflow: hidden;
  }
  .code {
    margin: 0;
  }
}
</style>
